(function(namespace){
	namespace.ConfirmBox = function()
	{
		var me = this;
		this.html = document.getElementsByTagName('html')[0];
		this.mask = document.createElement('div');
		this.mask.className = 'confirm_mask';
		this.mask.style.cssText = 'position:absolute; top:1px; left:0; width:100%; z-index:1000;';
		this.box_outer = document.createElement('div');
		this.box_outer.style.cssText = 'position:absolute;z-index:1001;top:0;left:0;width:100%;text-align:center;';
		this.box = document.createElement('span');
		this.box.className = 'confirm_box';
		this.box.style.cssText = '\
			display:inline-block!important;\
			min-width:40%!important;\
			_width:40%;\
			margin-top:10%!important;';
		this.inner = document.createElement('pre');
		this.inner.className = 'confirm_inner';
		this.inner.style.cssText = 'overflow-x:inherit;overflow-y:auto;';
		this.btn_ok = document.createElement('input');
		this.btn_ok.type = 'button';
		this.btn_ok.value = Lang('confirm');
		this.btn_no = document.createElement('input');
		this.btn_no.type = 'button';
		this.btn_no.value = Lang('cancel');
		
		this.box.appendChild(this.inner);
		this.box.appendChild(this.btn_no);
		this.box.appendChild(this.btn_ok);
		this.box_outer.appendChild(this.box);
		this.btn_ok.onclick = function(){me.handle_ok.call(self);me.hide();}
		this.btn_no.onclick = function(){me.handle_no.call(self);me.hide();}
		document.body.appendChild(this.mask);
		document.body.appendChild(this.box_outer);
	}
	namespace.ConfirmBox.prototype = {
		timer : null,
		isShow : false,
		program : [],
		handle_ok : function(){},
		handle_no : function(){},
		hide : function()
		{
			this.mask.style.display = 'none';
			this.box_outer.style.display = 'none';
			this.html.style.overflow = 'auto';
			this.timer = clearInterval(this.timer);
			if (typeof this.inner.innerText == typeof '')
			{
				this.inner.innerText = '';
			}
			else
			{
				this.inner.innerHTML = '';
			}
			this.inner.style.height = 'auto';
			this.inner.style.width = 'auto';
			this.isShow = false;
			if (this.program.length)
			{
				this.show(this.program.pop());
			}
		},
		show : function(options)
		{
			var me = this;
			if (this.isShow)
			{
				this.program.push(options);
				return;
			}
			if (options.ok instanceof Function)
			{
				this.handle_ok = options.ok;
			}
			if (options.no instanceof Function)
			{
				this.handle_no = options.no;
			}
			this.html.style.overflow = 'hidden';
			this.mask.style.display = 'block';
			this.box_outer.style.display = 'block';
			if (typeof this.inner.innerText == typeof '')
			{
				this.inner.innerText = options.msg;
			}
			else
			{
				this.inner.innerHTML = options.msg;
			}
			this.inner.style.width = (((me.box.offsetWidth>me.inner.scrollWidth)?me.box.offsetWidth:me.inner.scrollWidth)+10) + 'px';//因為IE7...
			if (!this.timer)
			{
				this.timer = setInterval(function(){
					//標準瀏覽器使用 window.innerHeight, IE 使用document.documentElement.offsetHeight
					var height = (self.innerHeight)?self.innerHeight : self.document.documentElement.offsetHeight;
					me.box_outer.style.height = me.mask.style.height = height + 'px';
					if ((0.6*height) < me.inner.offsetHeight)
					{
						me.inner.style.height = (0.6*height)+'px';
					}
					
					var scrollTop = (self.document.documentElement && self.document.documentElement.scrollTop>self.document.body.scrollTop)?self.document.documentElement.scrollTop:self.document.body.scrollTop;
					me.mask.style.top = scrollTop+'px';
					me.box_outer.style.top = scrollTop+'px';
				},20);
			}
			this.isShow = true;
		}
	};
	namespace.ConfirmBox.instance = function()
	{
		if (!this._instance && document.body)
		{
			this._instance = new this;
		}
		return this._instance;
	}

	this.new_confirm = function(str, handle_ok, handle_no)
	{
		var o = namespace.ConfirmBox.instance();
		if (!o)
		{
			return this.confirm(str);
		}
		o.show({'msg':str,'ok':handle_ok, 'no':handle_no});
	}
})();